package com.kmzx.one.project.controller;

import com.kmzx.one.common.annotation.ApiAuth;
import com.kmzx.one.common.utils.MyAssert;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.others.dto.CartDiscountInfo;
import com.kmzx.one.project.others.dto.ProdOutInfo;
import com.kmzx.one.project.others.dto.UserCartInfo;
import com.kmzx.one.project.others.dto.UserCartLimitInfo;
import com.kmzx.one.project.service.UserCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "快美甄选_[ 购物车_接口 ]")
@RestController
public class UserCartController extends BaseController {


    @Autowired
    UserCartService userCartService;

    @ApiOperation(value = "1.我的购物车列表", notes = "我的购物车列表")
    @GetMapping(value = "cart/mine", produces = "application/json;charset=UTF-8")
    public BaseResult findMyShoppingCart(String userId, Integer pageNum, Integer pageSize) {
        try {
            List<UserCartInfo> cart = userCartService.findMyShoppingCart();
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(cart);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    /**
     * @param cartId
     * @param count
     * @return
     */
    @ApiOperation(value = "2.增加减物品", notes = "what 0 加购物车 1 减购物车")
    @GetMapping(value = "cart/addOrSub", produces = "application/json;charset=UTF-8")
    public BaseResult addOrSubtractGoods(String cartId, Integer count) {
        try {
            MyAssert.notNullObject(cartId, "cartId 为空");
            MyAssert.notNullObject(count, "count 为空");
            int cart = userCartService.doAddOrSubtractGoods(cartId, count);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(cart);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    /**
     * 加入购物车
     *
     * @param userId
     * @param productId
     * @param skuId
     * @param count
     * @return
     */
    @ApiOperation(value = "3.加入购物车", notes = "加入购物车")
    @GetMapping(value = "cart/addToCart", produces = "application/json;charset=UTF-8")
    public BaseResult addToCart(String userId, String productId, String skuId,String tradeNum, Integer count) {
        try {
            MyAssert.notNullObject(userId, "userId 为空");
            MyAssert.notNullObject(productId, "productId 为空");
            MyAssert.notNullObject(skuId, "skuId 为空");
            MyAssert.notNullObject(count, "count 为空");
//            int cart = userCartService.doAddCart(userId, productId, skuId, count);
            int cart = userCartService.doAddCart2(userId, productId, skuId,tradeNum, count);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(cart);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }

    }

    /**
     * 删除购物车中商品
     *
     * @param userId
     * @param cartIdArr
     * @return
     */
    @ApiOperation(value = "4.删除购物车中商品", notes = "删除购物车中商品")
    @PostMapping(value = "cart/delCart", produces = "application/json;charset=UTF-8")
    public BaseResult delUserCartGoods(String userId, String[] cartIdArr) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullArray(cartIdArr, "cartIdArr 不可为空");
            int cart = userCartService.doDelCart(userId, cartIdArr);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(cart);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "5.优惠等级", notes = "优惠等级",position = -1)
    @RequestMapping(value = "cart/discount", produces = "application/json;charset=UTF-8",method = {RequestMethod.GET,RequestMethod.POST})
    public BaseResult getMyDiscountInfo(String userId) {

        try {
            MyAssert.notNullObject(userId, "userId 为null");
            CartDiscountInfo info = userCartService.findDiscountInfo(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }



    /**
     * 获取skuId
     *
     * @param skuId
     * @return
     */
//    @ApiOperation(value = "6.获取商品的属性 库存量", notes = "获取商品的属性 库存量")
//    @GetMapping(value = "cart/getSkuNum", produces = "application/json;charset=UTF-8")
    public BaseResult getSkuStores(String skuId) {
        try {
            Integer info = userCartService.findSkuIdNum(skuId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiAuth
    @ApiOperation(value = "6.获取推荐商品", notes = "获取推荐商品",position = -1)
    @GetMapping(value = "cart/recommend", produces = "application/json;charset=UTF-8")
    public BaseResult getRecommendProdList(@RequestParam("pageNum") Integer pageNum,
                                           @RequestParam("pageSize") Integer pageSize, String userId){
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            List<ProdOutInfo> info = userCartService.getRecommendProdList(userId,pageNum,pageSize);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "7.校验商品可否购买", notes = "校验商品可否购买",position = -1)
    @GetMapping(value = "cart/checkGoods", produces = "application/json;charset=UTF-8")
    public BaseResult checkCartCanBuy(String[] ids,String userId){
        try {
//            MyAssert.notNullObject(ids, "userId 为null");
            MyAssert.notNullObject(userId, "userId 为null");
            boolean info = userCartService.checkIdsIsOk(ids,userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "8.购物车数量求和", notes = "购物车数量求和",position = -1)
    @GetMapping(value = "cart/goodsNum", produces = "application/json;charset=UTF-8")
    public BaseResult totalNumCart(String userId){
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            int info = userCartService.totalCartNum(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }



    @ApiOperation(value = "9.购物车数量限制", notes = "购物车数量限制",position = -1)
    @GetMapping(value = "cart/goodsLimit", produces = "application/json;charset=UTF-8")
    public BaseResult findTradeLimits(){
        try {
            List<UserCartLimitInfo> info= userCartService.findUserCartLimitInfo();
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }



}
